package com.example.tabbar.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.tabbar.databinding.ItemConcentrationBinding
import com.example.tabbar.room.bean.Concentration

class ConcentrationAdapter: RecyclerView.Adapter<ConcentrationAdapter.ViewHolder>() {
    private var items: List<Concentration>? = null
    private var listener: ((Long) -> Unit)? = null
    fun setOnItemClickListener(listener: (Long) -> Unit) {
        this.listener = listener
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ItemConcentrationBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(binding)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = items?.get(position)
        if (item != null) {
            holder.size.text = item.size.toString() + "μm"
            holder.indication.text = item.indication
            holder.threshold.text = item.threshold
        }
    }

    override fun getItemCount(): Int {
        return items?.size ?: 0
    }

    fun setItems(items: List<Concentration>?) {
        this.items = items
        notifyDataSetChanged()
    }

    class ViewHolder(binding: ItemConcentrationBinding)
        : RecyclerView.ViewHolder(binding.root) {
        var size:TextView = binding.size
        var indication:TextView = binding.indication
        var threshold:TextView = binding.threshold
    }

}